package com.isoftstone.disk.obs;

import com.obs.services.ObsClient;
import com.obs.services.model.ObjectListing;
import com.obs.services.model.ObsObject;
import com.obs.services.model.TemporarySignatureRequest;
import com.obs.services.model.TemporarySignatureResponse;

import java.util.Date;
import java.util.List;

public class ObsMain {
    private static  final  String endpoint="obs.cn-north-4.myhuaweicloud.com";
    private static  final  String  ak = "HZBNCUNE63MH4CBLAAY6";
    private static  final  String  sk = "60wU8iGUm9oL68AHiaGNa14SigF8JkHXSV6y3j2Y";
    private static  final  String  bucketName = "javatest10";


    public static void main(String[] args) {
        ObsClient obsClient = new ObsClient(ak,sk,endpoint);

//        //文件列表
        ObjectListing list = obsClient.listObjects(bucketName);
        List<ObsObject> listObjects = list.getObjects();
        for(ObsObject obsObject:listObjects){
            System.out.println("obsKey:" + obsObject.getObjectKey());
            System.out.println("obsSize:"  + obsObject.getMetadata().getContentLength());
        }
//
//        //下载文件
//        ObsObject obsObject = obsClient.getObject(bucketName, "a1.jpg");
//        InputStream is = obsObject.getObjectContent();
//        InputStreamReader inputStreamReader = new InputStreamReader(is);
//        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//        byte[] block = new byte[1024];
//        Integer length = 0;
//        try(  FileOutputStream fileOutputStream =  new FileOutputStream("D:\\logfile\\a1.jpg");
//        ){
//            while ((length =  is.read(block)) > 0 ){
//                fileOutputStream.write(block,0,length);
//                fileOutputStream.flush();
//            }
//        }catch (IOException e1){
//            e1.printStackTrace();
//        }
//
//        //上传文件
//        try (FileInputStream inputStream = new FileInputStream("D:\\logfile\\a1.jpg");){
//            obsClient.putObject(bucketName,"a4.jpg",inputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
       //文件的删除
//        String objKey = "a4.jpg";
//        if(obsClient.doesObjectExist(bucketName,objKey)){
//            obsClient.deleteObject(bucketName,objKey);
//        }
        //文件的分享
//        String obsKey = "a4.jpg";
//        TemporarySignatureRequest request = new TemporarySignatureRequest();
//        request.setBucketName(bucketName);
//        request.setObjectKey(obsKey);
//        request.setRequestDate(new Date());
//        request.setExpires(60*60);
//        TemporarySignatureResponse signature = obsClient.createTemporarySignature(request);
//        String url = signature.getSignedUrl();
//        System.out.println(url);
        String obsKey = "zhumu_1.mp4";
        TemporarySignatureRequest request = new TemporarySignatureRequest();
        request.setBucketName(bucketName);
        request.setObjectKey(obsKey);
        request.setRequestDate(new Date());
        request.setExpires(60*60);
        TemporarySignatureResponse signature = obsClient.createTemporarySignature(request);
        String url = signature.getSignedUrl();
        System.out.println(url);
    }
}
